package com.zhiyou100.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhiyou100.ajaxinfo.Data;
import com.zhiyou100.ajaxinfo.SeriesData;
import com.zhiyou100.ajaxinfo.Title;
import com.zhiyou100.model.Course;
import com.zhiyou100.model.Video;
import com.zhiyou100.service.CourseService;

@Controller
@RequestMapping("/admin/statistics")
public class AdminStatisticsController {
	@Autowired
	private CourseService cs;
	@RequestMapping("/show")
	public String showData() {
		return "forward:/back/statistics/chart.jsp";
	}
	@RequestMapping("/manage")
	@ResponseBody
	public Map<String, Object> Statistics() {
		List<Course> courses = cs.selectCourseAndVideo();
		List<Data> data = new ArrayList<>();
		for (Course course : courses) {
			
			Data d = new Data();
			List<Video> video = course.getVideo();
			double i = 0;
			double avg=0;
			for (Video v : video) {
				if(v.getVideo_play_times()==null) {
					i=i+1;
				}else {
					i = i + v.getVideo_play_times();
				}
				
			}
			d.setName(course.getCourse_name());
			if(video.size()!=0) {
				avg = i/video.size();
			}
			
			d.setY(avg);
			data.add(d);
		}
		Title title = new Title();
		title.setText("课程平均播放次数");
		
		
		
		SeriesData  seriesData = new SeriesData();
		seriesData.setColorByPoint(true);
		seriesData.setData(data);
		seriesData.setName("平均播放次数");
		List<SeriesData> SeriesData = new ArrayList<>();
		SeriesData.add(seriesData);
		Map<String, Object> maps = new HashMap<>();
		maps.put("seriesdata", SeriesData);
		maps.put("title", title);
		return maps;
	}
}
